<?php
/**
 * 管理系统内存储的产品信息 或新建
 *
 * @author xyma
 *
 */
class NodeController extends Controller
{
	function __construct(){
		View::setLayout('admin');
	}

	function show()
	{
		$protos	=	Proto::getList();
		$this->assign('protos',	$protos);
		
		$where	= array();
		$key	= array();
		$pid	= fetch_request('pro_id','int');
		if($pid){
			$where['pro_id']	= $pid;
			$key[]	= "pro_id={$pid}";
		}
		$nodes	= NodeHelper::getList($where, 'node_id DESC', true);
		$this->assign('nodes',	$nodes);
		
		$this->display();
	}
	
	function add()
	{
		$pro_id	=	fetch_get('pid', 'int');
		$proto	=	ProtoHelper::load($pro_id);
		if(!$proto){
			Show::warning('请选择产品原型');
		}
		
		if(!empty($_POST))
		{
			$key	=	!empty($_SESSION['node_imgs']) ? $_SESSION['node_imgs'] : array();
			//先保存上传的文件
			if(!empty($_FILES['node_img']['tmp_name']))
			{
				foreach($_FILES['node_img']['tmp_name'] as $k=>$file)
				{
					if(is_uploaded_file($file))
					{
						$key[]	=	ImageStorageHelper::addStock(array(
								'name'	=>	$_FILES['node_img']['name'][$k],
								'type'	=>	$_FILES['node_img']['type'][$k],
								'tmp_name'	=>	$_FILES['node_img']['tmp_name'][$k],
								'error'	=>	$_FILES['node_img']['error'][$k],
								'size'	=>	$_FILES['node_img']['size'][$k],
						));
					}
				}
			}
			$_SESSION['node_imgs']	=	$key;
			
			if(!fetch_post('node_title'))
			{
				Show::warning('标题不能为空', 'SELF');
			}
			
			$attrs		=	$proto['attrs'];
			$node_attrs	=	array_intersect_key(fetch_post('attr', 'array'), $attrs);
			
			//自动生成“描述”部分
			$node_desc	=	fetch_post('node_desc');
			if(!$node_desc){
				foreach($attrs as $a){
					if($a['type']==0 && $a['b_type']==4){
						$node_desc	=	empty($_POST['attr'][$a['attr_id']])
											? ''
											: FilterHelper::strict($_POST['attr'][$a['attr_id']]);
					}
				}
			}
			
			//保存主要属性
			$node_id	=	Node::add(array(
				'pro_id'		=>	$proto['pro_id'],
				'node_title'	=>	FilterHelper::strict(fetch_post('node_title')),
				'node_desc'		=>	FilterHelper::strict($node_desc),
				'node_img'		=>	join(',', $key),
				'attrs'			=>	serialize($node_attrs),
			));
			if(!$node_id){
				Show::error('添加产品失败', 'SELF');
			}
			
			//保存hash类属性部分，便于搜索
			foreach($attrs as $attr_id=>$a)
			{
				if(in_array($a['type'], array('1','2')))
				{
					NodeHash::store($node_id, isset($node_attrs[$attr_id])?$node_attrs[$attr_id]:null);
				}
			}
			
			//修正storage.node_id
			foreach($key as $sid){
				Storage::update($sid, array('node_id'=>$node_id), null);
			}
			
			unset($_SESSION['node_imgs'], $key);
			Show::notice('添加成功', 'node');
		}
		
		$this->assign('proto',		$proto);
		$this->assign('proto_show',	ProtoHelper::pack($proto));
	
		if(!empty($_SESSION['node_imgs'])){
			$this->assign('lost_imgs',	$_SESSION['node_imgs']);
		}
		
		$this->display();
	}

	function mod()
	{
		$node_id	=	fetch_get('id', 'int');
		$node		=	NodeHelper::load($node_id);
		if(!$node){
			Show::warning('请选择产品');
		}
		$proto		=	ProtoHelper::load($node['pro_id']);
		
		if(!empty($_POST))
		{
			$key	=	!empty($_SESSION['node_imgs']) ? $_SESSION['node_imgs'] : array();
			//先保存上传的文件
			if(!empty($_FILES['node_img']['tmp_name']))
			{
				foreach($_FILES['node_img']['tmp_name'] as $k=>$file)
				{
					if(is_uploaded_file($file))
					{
						$key[]	=	ImageStorageHelper::addStock(array(
								'name'	=>	$_FILES['node_img']['name'][$k],
								'type'	=>	$_FILES['node_img']['type'][$k],
								'tmp_name'	=>	$_FILES['node_img']['tmp_name'][$k],
								'error'	=>	$_FILES['node_img']['error'][$k],
								'size'	=>	$_FILES['node_img']['size'][$k],
								'node_id'	=>	$node_id,
						));
					}
				}
			}
			$_SESSION['node_imgs']	=	$key;

			//保存主要属性
			$attrs		=	$proto['attrs'];
			$node_attrs	=	array_intersect_key(fetch_post('attr', 'array'), $attrs);
			if(!Node::update($node_id, array(
				'node_title'	=>	FilterHelper::strict(fetch_post('node_title')),
				'node_desc'		=>	FilterHelper::strict(fetch_post('node_desc')),
				'node_img'		=>	join(',', array_merge($key, $node['node_img'])),
				'attrs'			=>	serialize($node_attrs),
			))){
				Show::error('修改产品信息失败', 'SELF');
			}

			//保存hash类属性部分，便于搜索
			NodeHash::deleteByNode($node_id);
			foreach($attrs as $attr_id=>$a)
			{
				if(in_array($a['type'], array('1','2')))
				{
					NodeHash::store($node_id, isset($node_attrs[$attr_id])?$node_attrs[$attr_id]:null);
				}
			}
			
			unset($_SESSION['node_imgs']);
			Show::notice('修改成功', 'node');
		}
		
		$this->assign('node',		$node);
		$this->assign('proto',		$proto);
		$this->assign('proto_show',	ProtoHelper::pack($proto, $node['attrs']));
		
		if(!empty($_SESSION['node_imgs'])){
			$this->assign('lost_imgs',	$_SESSION['node_imgs']);
		}
		
		$this->display();
	}
    
	/**
	 * 预览产品小样
	 */
	function prev()
	{
		$node_id	=	fetch_get('id', 'int');
		$node		=	NodeHelper::load($node_id);
		if(!$node_id || !$node)
		{
			Show::warning('无此产品信息');
		}
		
		$proto	= ProtoHelper::load($node['pro_id']);
		$node['attrs']	= ProtoHelper::inflateAttr($proto['attrs'], $node['attrs']);

		$this->assign('proto',	$proto);
		$this->assign('node',	$node);
		
		$this->display();
	}

	/**
	 * 编辑产品图片
	 */
	function img()
	{
		$node_id	=	fetch_get('id', 'int');
		$node		=	NodeHelper::load($node_id);
		if(!$node_id || !$node){
			Show::warning('无此产品信息');
		}

		$this->assign('node',	$node);
		$this->display();
	}
	
	/**
	 * 保存客户端提交的排序结果
	 */
	function img_sort()
	{
		$node_id	=	fetch_get('id', 'int');
		$keys		=	fetch_post('keys');
		if(!$node_id || !$keys){
			die('缺少必要的参数');
		}

		$keys	=	explode(':', $keys);
		$node	=	NodeHelper::load($node_id);
		if(array_diff($node['node_img'], $keys)){
			die('排序图片与原有图片数量不一致，请刷新再排序重试');
		}

		die(
			Node::update($node_id, array('node_img'=>join(',', $keys)))
			? '保存成功'
			: '失败，请重试'
		);
	}

	/**
	 * 删除图片
	 */
	function img_del()
	{
		$node_id	=	fetch_get('id', 'int');
		$key		=	fetch_get('key');
		if(!$node_id || !$key){
			die('缺少必要的参数');
		}

		$node		=	NodeHelper::load($node_id);
		if(!in_array($key, $node['node_img'])){
			die('目标图片不存在');
		}
		if( Node::update($node_id, array('node_img'=>join(',',array_diff($node['node_img'],array($key))))) )
		{
			ImageStorageHelper::dropStock($key);
			die('ok');
		}
		else{
			die('失败，请重试');
		}
	}

}
?>